// pages/cart/index.js
import {clearCart, getCartList, getCartTotal, resetCart} from '../../service/index.js'

Page({
    /**
     * 页面的初始数据
     */
    data: {
        cartList: [],
        checkedAll: false,
        cartTotal: null,
        edit: false,
        totalPrice: 0
    },
    onLoad(options) {
        this.getCartList()
    },
    changeEdit: function () {
        this.setData({
            edit: !this.data.edit
        })
        console.log(this.data.edit)
    },
    deleteAll: function () {
        clearCart(this.data.cartList.filter(v => v.checked).map(v => v.id), (res) => {
            this.calculateTotalPrice()
            this.getCartList()
        })
    },
    getCartList: function () {
        getCartList((res) => {
            const {list} = res.data
            this.setData({
                cartList: list
            })
            this.calculateTotalPrice()
        })
        getCartTotal((res) => {
            // console.log(res,'商品总数')
            const {cartTotal} = res.data
            console.log(cartTotal, '商品总数')
            this.setData({
                cartTotal: cartTotal
            })
        })
    },
    onChangeAll(event) {
        const isChecked = event.detail; // 这应该是一个布尔值，表示复选框的新状态
        if (isChecked) {
            const data = this.data.cartList.map(v => {
                v.checked = true
                return v
            });
            this.setData({
                cartList: data,
                checkedAll: true
            });
        } else {
            const data = this.data.cartList.map(v => {
                v.checked = false
                return v
            });
            this.setData({
                cartList: data,
                checkedAll: false
            });
        }
        this.calculateTotalPrice()
    },
    onChange(event) {
        const id = event.currentTarget.dataset.id;
        const isChecked = event.detail;

        if (isChecked) {
            const data = this.data.cartList.map(v => {
                if (v.id === id) {
                    v.checked = true
                }
                return v
            })
            this.setData({
                cartList: data
            });
        } else {
            const data = this.data.cartList.map(v => {
                if (v.id === id) {
                    v.checked = false
                }
                return v
            })
            this.setData({
                cartList: data,
                checkedAll: false
            });
        }
        this.calculateTotalPrice()
    },
    calculateTotalPrice() {
        const totalPrice = this.data.cartList.filter(v => v.checked).reduce((acc, item) => acc + (Number(item.goods.goods_price_max)*Number(item.goods_num)), 0) * 100;
        const cartTotal = this.data.cartList.filter(v => v.checked).reduce((acc, item) => acc + Number(item.goods_num), 0);
        this.setData({
            totalPrice: totalPrice,
            cartTotal: cartTotal
        });
    },
    increaseCount(e) {
        const item = e.currentTarget.dataset.item
        resetCart(item.goods_id, item.goods_sku_id, ++item.goods_num, (res) => {
            this.getCartList()
        })
    },
    decreaseCount(e) {
        const item = e.currentTarget.dataset.item
        resetCart(item.goods_id, item.goods_sku_id, --item.goods_num, (res) => {
            this.getCartList()
        })
    },
    onClickButton(){
        wx.navigateTo({
            url:`/pages/orderSettlement/index?cartIds=${this.data.cartList.filter(v=>v.checked).map(v=>v.id).join(',')}&cartTotal=${this.data.cartTotal}&cartList=${JSON.stringify(this.data.cartList.filter(v=>v.checked))}&totalPrice=${this.data.totalPrice}`
        })
    }
})
